﻿using System;
using LuaInterface;

public class ByteArrayWrap
{
	public static void Register(IntPtr L)
	{
		LuaMethod[] regs = new LuaMethod[]
		{
			new LuaMethod("Create", Create),
			new LuaMethod("ReadByte", ReadByte),
			new LuaMethod("ReadBool", ReadBool),
			new LuaMethod("ReadShort", ReadShort),
			new LuaMethod("ReadInt", ReadInt),
			new LuaMethod("ReadLong", ReadLong),
			new LuaMethod("ReadFloat", ReadFloat),
			new LuaMethod("ReadDouble", ReadDouble),
			new LuaMethod("ReadString", ReadString),
			new LuaMethod("WriteByte", WriteByte),
			new LuaMethod("WriteBool", WriteBool),
			new LuaMethod("WriteShort", WriteShort),
			new LuaMethod("WriteInt", WriteInt),
			new LuaMethod("WriteLong", WriteLong),
			new LuaMethod("WriteFloat", WriteFloat),
			new LuaMethod("WriteDouble", WriteDouble),
			new LuaMethod("WriteString", WriteString),
			new LuaMethod("ToArray", ToArray),
			new LuaMethod("New", _CreateByteArray),
			new LuaMethod("GetClassType", GetClassType),
		};

		LuaField[] fields = new LuaField[]
		{
			new LuaField("Position", get_Position, set_Position),
			new LuaField("Length", get_Length, null),
		};

		LuaScriptMgr.RegisterLib(L, "ByteArray", typeof(ByteArray), regs, fields, typeof(object));
	}

	[MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
	static int _CreateByteArray(IntPtr L)
	{
		int count = LuaDLL.lua_gettop(L);

		if (count == 0)
		{
			ByteArray obj = new ByteArray();
			LuaScriptMgr.PushObject(L, obj);
			return 1;
		}
		else if (count == 1)
		{
			byte[] objs0 = LuaScriptMgr.GetArrayNumber<byte>(L, 1);
			ByteArray obj = new ByteArray(objs0);
			LuaScriptMgr.PushObject(L, obj);
			return 1;
		}
		else
		{
			LuaDLL.luaL_error(L, "invalid arguments to method: ByteArray.New");
		}

		return 0;
	}

	static Type classType = typeof(ByteArray);

	[MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
	static int GetClassType(IntPtr L)
	{
		LuaScriptMgr.Push(L, classType);
		return 1;
	}

	[MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
	static int get_Position(IntPtr L)
	{
		object o = LuaScriptMgr.GetLuaObject(L, 1);
		ByteArray obj = (ByteArray)o;

		if (obj == null)
		{
			LuaTypes types = LuaDLL.lua_type(L, 1);

			if (types == LuaTypes.LUA_TTABLE)
			{
				LuaDLL.luaL_error(L, "unknown member name Position");
			}
			else
			{
				LuaDLL.luaL_error(L, "attempt to index Position on a nil value");
			}
		}

		LuaScriptMgr.Push(L, obj.Position);
		return 1;
	}

	[MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
	static int get_Length(IntPtr L)
	{
		object o = LuaScriptMgr.GetLuaObject(L, 1);
		ByteArray obj = (ByteArray)o;

		if (obj == null)
		{
			LuaTypes types = LuaDLL.lua_type(L, 1);

			if (types == LuaTypes.LUA_TTABLE)
			{
				LuaDLL.luaL_error(L, "unknown member name Length");
			}
			else
			{
				LuaDLL.luaL_error(L, "attempt to index Length on a nil value");
			}
		}

		LuaScriptMgr.Push(L, obj.Length);
		return 1;
	}

	[MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
	static int set_Position(IntPtr L)
	{
		object o = LuaScriptMgr.GetLuaObject(L, 1);
		ByteArray obj = (ByteArray)o;

		if (obj == null)
		{
			LuaTypes types = LuaDLL.lua_type(L, 1);

			if (types == LuaTypes.LUA_TTABLE)
			{
				LuaDLL.luaL_error(L, "unknown member name Position");
			}
			else
			{
				LuaDLL.luaL_error(L, "attempt to index Position on a nil value");
			}
		}

		obj.Position = (int)LuaScriptMgr.GetNumber(L, 3);
		return 0;
	}

	[MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
	static int Create(IntPtr L)
	{
		LuaScriptMgr.CheckArgsCount(L, 0);
		ByteArray o = ByteArray.Create();
		LuaScriptMgr.PushObject(L, o);
		return 1;
	}

	[MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
	static int ReadByte(IntPtr L)
	{
		LuaScriptMgr.CheckArgsCount(L, 1);
		ByteArray obj = (ByteArray)LuaScriptMgr.GetNetObjectSelf(L, 1, "ByteArray");
		int o = obj.ReadByte();
		LuaScriptMgr.Push(L, o);
		return 1;
	}

	[MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
	static int ReadBool(IntPtr L)
	{
		LuaScriptMgr.CheckArgsCount(L, 1);
		ByteArray obj = (ByteArray)LuaScriptMgr.GetNetObjectSelf(L, 1, "ByteArray");
		bool o = obj.ReadBool();
		LuaScriptMgr.Push(L, o);
		return 1;
	}

	[MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
	static int ReadShort(IntPtr L)
	{
		LuaScriptMgr.CheckArgsCount(L, 1);
		ByteArray obj = (ByteArray)LuaScriptMgr.GetNetObjectSelf(L, 1, "ByteArray");
		int o = obj.ReadShort();
		LuaScriptMgr.Push(L, o);
		return 1;
	}

	[MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
	static int ReadInt(IntPtr L)
	{
		LuaScriptMgr.CheckArgsCount(L, 1);
		ByteArray obj = (ByteArray)LuaScriptMgr.GetNetObjectSelf(L, 1, "ByteArray");
		int o = obj.ReadInt();
		LuaScriptMgr.Push(L, o);
		return 1;
	}

	[MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
	static int ReadLong(IntPtr L)
	{
		LuaScriptMgr.CheckArgsCount(L, 1);
		ByteArray obj = (ByteArray)LuaScriptMgr.GetNetObjectSelf(L, 1, "ByteArray");
		long o = obj.ReadLong();
		LuaScriptMgr.Push(L, o);
		return 1;
	}

	[MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
	static int ReadFloat(IntPtr L)
	{
		LuaScriptMgr.CheckArgsCount(L, 1);
		ByteArray obj = (ByteArray)LuaScriptMgr.GetNetObjectSelf(L, 1, "ByteArray");
		float o = obj.ReadFloat();
		LuaScriptMgr.Push(L, o);
		return 1;
	}

	[MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
	static int ReadDouble(IntPtr L)
	{
		LuaScriptMgr.CheckArgsCount(L, 1);
		ByteArray obj = (ByteArray)LuaScriptMgr.GetNetObjectSelf(L, 1, "ByteArray");
		double o = obj.ReadDouble();
		LuaScriptMgr.Push(L, o);
		return 1;
	}

	[MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
	static int ReadString(IntPtr L)
	{
		LuaScriptMgr.CheckArgsCount(L, 1);
		ByteArray obj = (ByteArray)LuaScriptMgr.GetNetObjectSelf(L, 1, "ByteArray");
		string o = obj.ReadString();
		LuaScriptMgr.Push(L, o);
		return 1;
	}

	[MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
	static int WriteByte(IntPtr L)
	{
		LuaScriptMgr.CheckArgsCount(L, 2);
		ByteArray obj = (ByteArray)LuaScriptMgr.GetNetObjectSelf(L, 1, "ByteArray");
		byte arg0 = (byte)LuaScriptMgr.GetNumber(L, 2);
		obj.WriteByte(arg0);
		return 0;
	}

	[MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
	static int WriteBool(IntPtr L)
	{
		LuaScriptMgr.CheckArgsCount(L, 2);
		ByteArray obj = (ByteArray)LuaScriptMgr.GetNetObjectSelf(L, 1, "ByteArray");
		bool arg0 = LuaScriptMgr.GetBoolean(L, 2);
		obj.WriteBool(arg0);
		return 0;
	}

	[MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
	static int WriteShort(IntPtr L)
	{
		LuaScriptMgr.CheckArgsCount(L, 2);
		ByteArray obj = (ByteArray)LuaScriptMgr.GetNetObjectSelf(L, 1, "ByteArray");
		short arg0 = (short)LuaScriptMgr.GetNumber(L, 2);
		obj.WriteShort(arg0);
		return 0;
	}

	[MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
	static int WriteInt(IntPtr L)
	{
		LuaScriptMgr.CheckArgsCount(L, 2);
		ByteArray obj = (ByteArray)LuaScriptMgr.GetNetObjectSelf(L, 1, "ByteArray");
		int arg0 = (int)LuaScriptMgr.GetNumber(L, 2);
		obj.WriteInt(arg0);
		return 0;
	}

	[MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
	static int WriteLong(IntPtr L)
	{
		LuaScriptMgr.CheckArgsCount(L, 2);
		ByteArray obj = (ByteArray)LuaScriptMgr.GetNetObjectSelf(L, 1, "ByteArray");
		long arg0 = (long)LuaScriptMgr.GetNumber(L, 2);
		obj.WriteLong(arg0);
		return 0;
	}

	[MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
	static int WriteFloat(IntPtr L)
	{
		LuaScriptMgr.CheckArgsCount(L, 2);
		ByteArray obj = (ByteArray)LuaScriptMgr.GetNetObjectSelf(L, 1, "ByteArray");
		float arg0 = (float)LuaScriptMgr.GetNumber(L, 2);
		obj.WriteFloat(arg0);
		return 0;
	}

	[MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
	static int WriteDouble(IntPtr L)
	{
		LuaScriptMgr.CheckArgsCount(L, 2);
		ByteArray obj = (ByteArray)LuaScriptMgr.GetNetObjectSelf(L, 1, "ByteArray");
		double arg0 = (double)LuaScriptMgr.GetNumber(L, 2);
		obj.WriteDouble(arg0);
		return 0;
	}

	[MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
	static int WriteString(IntPtr L)
	{
		LuaScriptMgr.CheckArgsCount(L, 2);
		ByteArray obj = (ByteArray)LuaScriptMgr.GetNetObjectSelf(L, 1, "ByteArray");
		string arg0 = LuaScriptMgr.GetLuaString(L, 2);
		obj.WriteString(arg0);
		return 0;
	}

	[MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
	static int ToArray(IntPtr L)
	{
		LuaScriptMgr.CheckArgsCount(L, 1);
		ByteArray obj = (ByteArray)LuaScriptMgr.GetNetObjectSelf(L, 1, "ByteArray");
		byte[] o = obj.ToArray();
		LuaScriptMgr.PushArray(L, o);
		return 1;
	}
}

